<title>组局管理</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>组局管理</cite></a>
    <a><cite>组局列表</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
      <div class="layui-form-item">

        <div class="layui-inline">
          <label class="layui-form-label">发起人</label>
          <div class="layui-input-block">
            <input type="text" name="nickName" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">产品名</label>
          <div class="layui-input-block">
            <input type="text" name="productName" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">创建人</label>
          <div class="layui-input-block">
            <select name="role">
              <option value="">不限</option>
              <option value="1">玩家</option>
              <option value="0">管理员</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">状态</label>
          <div class="layui-input-block">
            <select name="state">
              <option value="">不限</option>
              <option value="0">组局中</option>
              <option value="1">已成局</option>
              <option value="2">已结束</option>
              <option value="3">已解散</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-admin" data-type="add" id="add">添加</button>
      </div>

      <table id="table-instance" lay-filter="table-instance"></table>

      <script type="text/html" id="table-useradmin-admin">
       <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info"><i class="layui-image layui-image"></i>查看拼车记录</a>
       <a class="layui-btn layui-btn layui-btn-xs" lay-event="edit"><i class="layui-image layui-image-edit"></i>编辑</a>
       <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="dm"><i class="layui-image layui-image"></i>分配Dm</a>
       <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-image "></i>删除</a>
      </script>
    </div>
  </div>
</div>

<style type="text/css">
  .layui-table-cell {
    height: auto;
    line-height: 28px;
  }
</style>

<script>
  layui.use('useradmin', layui.factory('useradmin')).use(['admin', 'useradmin', 'table', 'laydate', 'layedit', 'upload'], function () {
    var $ = layui.$
      , admin = layui.admin
      , view = layui.view
      , setter = layui.setter
      , table = layui.table
      , form = layui.form
      , layedit = layui.layedit
      , laydate = layui.laydate
      , upload = layui.upload


    layedit.set({
      uploadImage: {
        url: setter.request.baseUrl + '/upload/uploadEdit'
      }
    });

    form.render(null, 'layadmin-useradmin-formlist');
    //监听搜索
    form.on('submit(LAY-user-back-search)', function (data) {
      var field = data.field;
      //执行重载
      table.reload('table-instance', {
        where: field
      });
    });
    //事件

    var active = {
      add: function () {
        admin.popup({
          title: '添加组局'
          , area: ['700px', '650px']
          , id: 'LAY-popup-useradmin-add'
          , success: function (layero, index) {
            view(this.id).render('group/form').done(function () {
              form.render(null, 'layuiadmin-form-admin');
              //监听提交

              layui.laydate.render({
                elem: '#appointmentTime'//指定元素
              });
              form.on('submit(LAY-user-back-submit)', function (data) {
                var field = data.field; //获取提交的字段
                field.type = 0
                admin.req({
                  url: setter.request.baseUrl + "group/save",
                  data: JSON.stringify(Object.assign(data, field)),
                  type: "post",
                  dataType: "json"
                  , contentType: "application/json",
                  done: function (res) {
                    if (res.msg != "success") {
                      layer.msg("人数太多了")
                    }
                    if (res.msg == "success") {
                      layer.close(index); //执行关闭 
                      layui.table.reload("table-instance");
                    }
                  },
                });

              });
            });
          }
        });
      }
    };
    table.render({
      elem: "#table-instance",
      url: setter.request.baseUrl + "group/list",
      method: "get",
      headers: { token: layui.data("layuiAdmin").token },
      response: {
        statusName: "code",
        statusCode: 0,
        msgName: "msg",
        countName: "totalCount",
        dataName: "group",
      },
      cols: [
        [
          { field: "id", title: "id", sort: "true", width: "4%" },
          {
            field: "product", title: "产品信息", templet: function (d) {
              return "产品ID:" + d.productId + '</br>' + "产品名:" + d.productName + '</br>' + "产品游玩时间:"
                + d.productGameTime + '</br>' + "产品游玩人数:" + d.productGameNumber + "人" + '</br>' + "产品介绍:"
                + d.introduction + '</br>'
            }
          },
          {
            field: "playCount", title: "参与人数", width: "6%", templet: function (d) {
              return d.playCount + "/" + d.productGameNumber
            }
          },
          {
            field: "user", title: "发起方", width: "6%", templet: function (d) {
              return "id:" + d.userId + '</br>' + "姓名:" + d.nickName
            }
          },
          { field: "appointmentTime", title: "预约时间", width: "6%" },
          {
            field: "count", title: "玩本人数", width: "5%", templet: function (d) {
              return d.count + "人"
            }
          },
          { field: "content", title: "留言" },
          { field: "role", title: "创建人", templet: "#role", width: "5%" },
          { field: "state", title: "状态", templet: "#state", width: "6%" },
          {
            field: "dmName", title: "DM", width: "6%", templet: function (d) {
              if (d.dmName == null) {
                return "暂无DM"
              } else {
                return d.dmName
              }
            }
          },

          { field: "createdate", title: "创建时间", width: "10%" },
          { fixed: "right", title: "操作", toolbar: "#table-useradmin-admin" },
        ],
      ],
      page: true,
    });

    //删除模块
    table.on("tool(table-instance)", function (obj) {
      var data = obj.data;
      if (obj.event === "del") {
        layer.confirm("真的删除么", function (index) {
          delcardHandle({
            id: data.id,
          });
          obj.del();
          layer.close(index);
        });
      } else if (obj.event === "edit") {
        admin.popup({
          title: "编辑组局",
          area: ["1100px", "850px"],
          success: function (layero, index) {
            view(this.id)
              .render("group/form", data)
              .done(function () {
                let _data = data;
                form.render(null, "layuiadmin-form-admin");
                layui.laydate.render({
                  elem: '#appointmentTime'//指定元素
                });
                form.on('submit(LAY-user-back-submit)', function (data) {
                  var field = data.field;
                  admin.req({
                    url: setter.request.baseUrl + "group/edit",
                    data: JSON.stringify(Object.assign(_data, field)),
                    type: "post",
                    dataType: "json"
                    , contentType: "application/json",
                    done: function (res) {
                      if (res.msg != "success") {
                        layer.msg("人数太多了")
                      }
                      if (res.msg == "success") {
                        layer.close(index); //执行关闭 
                        layui.table.reload("table-instance");
                      }
                    },
                  });
                });
              });
          },
        });
      } else if (obj.event === "info") {
        window.location.href = `../#/group/listType.html?,` + data.id

      } else if (obj.event === "dm") {
        admin.popup({
          title: "分配DM",
          area: ["500px", "450px"],
          success: function (layero, index) {
            view(this.id)
              .render("group/dmform", data)
              .done(function () {
                let _data = data;
                form.render(null, "layuiadmin-form-admin");
                layui.laydate.render({
                  elem: '#appointmentTime'//指定元素
                });
                form.on('submit(LAY-user-back-submit)', function (data) {
                  var field = data.field;
                  editGroup(Object.assign(_data, field));
                  layer.close(index); //执行关闭 
                  layui.table.reload("table-instance");
                });
              });
          },
        });
      }
    });

    function editGroup(params) {
      admin.req({
        url: setter.request.baseUrl + "group/dm",
        data: JSON.stringify(params),
        type: "post",
        dataType: "json"
        , contentType: "application/json",
        done: function (res) {
          layui.table.reload("table-instance");
        },
      });
    }

    //删除
    function delcardHandle(params) {
      admin.req({
        url: setter.request.baseUrl + "group/delete",
        data: {
          ...params,
        },
        type: "get",
        done: function (res) {
          layer.msg(res.msg);
        },
      });
    }


    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });

</script>

<script type="text/html" id="role">
{{# if(d.role ==1){ }}
{{# return "用户" }}
{{# } }}
{{# if(d.role==0){ }}
{{# return "管理员" }}
{{# } }}
</script>

<script type="text/html" id="state">
  {{# if(d.state ==0){ }}
  {{# return "组局中" }}
  {{# } }}
  {{# if(d.state==1){ }}
  {{# return "已成局" }}
  {{# } }}
  {{# if(d.state ==2){ }}
  {{# return "已结束" }}
  {{# } }}
  {{# if(d.state==3){ }}
  {{# return "已解散" }}
  {{# } }}
</script>